//
//  FindViewController.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/28.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "FindViewController.h"
#import "RecommendModel.h"
#import "CommendCollectionViewCell.h"
#import "MusicListViewController.h"
#import "TopCollectionViewCell.h"
#import "AnnouncerCollectionViewCell.h"
#import "BestMusicListViewController.h"
#import "FindMoreViewController.h"
#import "BestListenViewController.h"
#import "SortDetailedViewController.h"
#import "BroadcasterViewController.h"
#import "ProgramViewController.h"
#import "TPIViewController.h"
#import "AnnouncerMoreViewController.h"

@interface FindViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,FindCollectionViewCellDelegage,TopCollectionViewCellDelegate,AnnouncerCollectionViewCellDelegate>
//menu数组
@property(nonatomic,strong)NSMutableArray *menuArr;
//menu位置提示
@property(nonatomic,strong)UIView *menuLocationView;
//当前点击的menu的第几个button
@property(nonatomic,assign)NSInteger menuClickIndex;
//左右滑动collectionView
@property(nonatomic,strong)UICollectionView *findCollectionView;
//请求地址
@property(nonatomic,strong)NSArray *requestUrlArr;
//菜单栏高度
@property(nonatomic,assign)NSInteger menuHeight;

@property(nonatomic,strong)NSMutableArray *specielArr;
@property(nonatomic,strong)NSMutableArray *nomalArr;

@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initMenu];
    [self initFindCollectionView];
}

/**
 *  初始化菜单栏
 */
- (void)initMenu
{
    // 菜单
    self.menuHeight = 45;
    NSArray *menuTitleArr = [NSArray arrayWithObjects:@"推荐",@"榜单",@"主播", nil];
    self.menuArr = [NSMutableArray array];
    for(NSInteger i = 0;i < menuTitleArr.count; i++){
        CGFloat x = FM_Screen_Width / menuTitleArr.count * i;
        CGFloat y = 64;
        CGFloat w = FM_Screen_Width / menuTitleArr.count;
        CGFloat h = FM_Screen_Pts_Height * self.menuHeight;
        UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        menuBtn.frame = CGRectMake(x, y, w, h);
        [menuBtn setTitle:[menuTitleArr objectAtIndex:i] forState:UIControlStateNormal];
        [menuBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        menuBtn.tag = 10000 + i;
        [menuBtn addTarget:self action:@selector(clickMenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [menuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            // 设置选中的button字体大小
            menuBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            self.menuClickIndex = 10000;
        }
        [self.view addSubview:menuBtn];
        [self.menuArr addObject:menuBtn];
    }

    // 菜单下的位置提示view
    CGFloat btnWidth = FM_Screen_Width / menuTitleArr.count;
    self.menuLocationView = [[UIView alloc] initWithFrame:CGRectMake(btnWidth/3, 64+FM_Screen_Pts_Height*self.menuHeight, btnWidth/3, 2)];
    self.menuLocationView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_menuLocationView];
}

/**
 *  菜单栏button点击回调
 *
 *  @param sender 点击的button
 */
- (void)clickMenuBtnAction:(UIButton *)sender
{
    // 还原menu颜色
    UIButton *oldMenuBtn = [self.menuArr objectAtIndex:(self.menuClickIndex-10000)];
    [oldMenuBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    // 还原menu字体大小
    oldMenuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    // 设置menu颜色
    NSInteger index = sender.tag-10000;
    UIButton *menuBtn = [self.menuArr objectAtIndex:index];
    [menuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.menuClickIndex = sender.tag;
    
    // 更新位置提示
    CGFloat btnWidth = FM_Screen_Width / self.menuArr.count;
    self.menuLocationView.frame = CGRectMake((index*btnWidth+btnWidth/3), 64+FM_Screen_Pts_Height*self.menuHeight, btnWidth/3, 2);
    
    // 设置选中的button字体大小
    menuBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    // 设置左右滑动collectionView偏移页码
    self.findCollectionView.contentOffset = CGPointMake(index * FM_Screen_Width, 0);
}

/**
 *  初始化最外层的collectionView
 */
- (void)initFindCollectionView
{
    UICollectionViewFlowLayout *findFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    findFlowLayout.itemSize = CGSizeMake(FM_Screen_Width, FM_Screen_Height-64-48-(FM_Screen_Pts_Height*self.menuHeight+self.menuLocationView.frame.size.height));
    findFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    findFlowLayout.minimumLineSpacing = 0;
    self.findCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.menuLocationView.frame.origin.y+2, FM_Screen_Width, FM_Screen_Height-64-48-(FM_Screen_Pts_Height*self.menuHeight+self.menuLocationView.frame.size.height)) collectionViewLayout:findFlowLayout];
    self.findCollectionView.pagingEnabled = YES;
    self.findCollectionView.dataSource = self;
    self.findCollectionView.delegate = self;
    self.findCollectionView.showsHorizontalScrollIndicator = NO;
    self.findCollectionView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    [self.view addSubview:_findCollectionView];
    
    // 推荐
    [self.findCollectionView registerClass:[CommendCollectionViewCell class] forCellWithReuseIdentifier:@"findItem"];
    // 榜单
    [self.findCollectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:@"topItem"];
    // 主播
    [self.findCollectionView registerClass:[AnnouncerCollectionViewCell class] forCellWithReuseIdentifier:@"announcerItem"];
}

// UICollectionViewDataSource 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.menuArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        CommendCollectionViewCell *commendCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"findItem" forIndexPath:indexPath];
        commendCell.delegate = self;
        return commendCell;
    } else if(indexPath.item == 1) {
        TopCollectionViewCell *topCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topItem" forIndexPath:indexPath];
        topCell.delegate = self;
        return topCell;
    } else {
        AnnouncerCollectionViewCell *announcerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"announcerItem" forIndexPath:indexPath];
        announcerCell.delegate = self;
        return announcerCell;
    }
}

// FindCollectionViewCellDelegage 协议方法
// 跳转至FM列表页的协议
- (void)musicListPage:(NSDictionary *)musicListDic
{
    MusicListViewController *musicListVC = [[MusicListViewController alloc]init];
    musicListVC.musicListDic = musicListDic;
   
    [self.navigationController pushViewController:musicListVC animated:YES];
}
// 跳转至精品听单详情页
- (void)bestMusicListPage:(NSDictionary *)bestMusicListDic
{
    BestMusicListViewController *bestMusicListVC = [[BestMusicListViewController alloc] init];
    bestMusicListVC.bestMusicDictionary = bestMusicListDic;
    [self.navigationController pushViewController:bestMusicListVC animated:YES];
}
// 跳转至发现更多页面
- (void)findMorePage
{
    FindMoreViewController *findMoreVC = [[FindMoreViewController alloc] init];
    [self.navigationController pushViewController:findMoreVC animated:YES];
}

// 跳转至精品单听更多页面
- (void)bestMorePage
{
    BestListenViewController *findMoreVC = [[BestListenViewController alloc] init];
    [self.navigationController pushViewController:findMoreVC animated:YES];
}

// 跳转至听..页面
- (void)listenPage:(NSString *)id title:(NSString *)title
{
    SortDetailedViewController *listenMoreVC = [[SortDetailedViewController alloc] init];
    listenMoreVC.num = id;
    listenMoreVC.title = title;
    [self.navigationController pushViewController:listenMoreVC animated:YES];
}

// 跳转至榜单详情页面
- (void)topPage:(NSString *)contentType key:(NSString *)key title:(NSString *)title type:(NSString *)type{
    if ([type isEqualToString:@"节目"]) {
        BroadcasterViewController *broadcasterVC = [[BroadcasterViewController alloc] init];
        broadcasterVC.title = title;
        broadcasterVC.contentType = contentType;
        broadcasterVC.key = key;
        [self.navigationController pushViewController:broadcasterVC animated:YES];
    }else{
        ProgramViewController *proadcasterVC = [[ProgramViewController alloc] init];
        proadcasterVC.title = title;
        proadcasterVC.contentType = contentType;
        proadcasterVC.key = key;
        [self.navigationController pushViewController:proadcasterVC animated:YES];
    }
}

// 跳转至主播详情页面
- (void)announcerPage:(NSDictionary *)dic
{
    TPIViewController *tpiVC = [[TPIViewController alloc] init];
    tpiVC.tpiDic = dic;
    [self.navigationController pushViewController:tpiVC animated:YES];
}
// 跳转至主播更多页面
- (void)announcerMorePage:(NSString *)key title:(NSString *)title
{
    AnnouncerMoreViewController *annMoreVC = [[AnnouncerMoreViewController alloc] init];
    annMoreVC.key = key;
    annMoreVC.titleStr = title;
    [self.navigationController pushViewController:annMoreVC animated:YES];
}

// UIScrollViewDelegate代理方法,根据偏移量设置menu选中状态
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / FM_Screen_Width;
    if (index != self.menuClickIndex) {
        // 还原menu颜色
        UIButton *oldMenuBtn = [self.menuArr objectAtIndex:(self.menuClickIndex-10000)];
        [oldMenuBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
        // 还原menu字体大小
        oldMenuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        // 设置menu颜色
        UIButton *menuBtn = [self.menuArr objectAtIndex:index];
        [menuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.menuClickIndex = 10000 + index;
        
        // 设置选中的button字体大小
        menuBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
        // 更新位置提示
        CGFloat btnWidth = FM_Screen_Width / self.menuArr.count;
        self.menuLocationView.frame = CGRectMake((index*btnWidth+btnWidth/3), 64+FM_Screen_Pts_Height*self.menuHeight, btnWidth/3, 2);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
